package com.nyist.myticket.core.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.nyist.myticket.core.enums.MsgCodeEnum;

import java.io.Serializable;

/**
 * 响应结果封装
 *
 * @author yangxiaogen
 * @version 1.0
 * @date 2021/01/05
 **/
public class ResponseResult<T> implements Serializable {

    private String code;

    private String msg;

    private T data;

    // 分页页数
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private int page;
    // 行数
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private int rows;

    //页数
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private int records;

    // 总记录数
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private int total;

    public ResponseResult() {
        this.code = MsgCodeEnum.SUCCESS.getCode();
        this.msg = MsgCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseResult<T> error() {
        return error(MsgCodeEnum.ERROR.getCode(), "系统内部错误");
    }


    public static <T> ResponseResult<T> error(String msg) {
        return error(MsgCodeEnum.ERROR.getCode(), msg);
    }

    public static <T> ResponseResult<T> error(String code, String msg) {
        return new ResponseResult<T>(code, msg);
    }

    public static <T> ResponseResult<T> error(String code, String msg, T data) {
        return new ResponseResult<T>(code, msg, data);
    }

    public static <T> ResponseResult<T> success() {
        return success("成功", null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return success("成功", data);
    }

    public static <T> ResponseResult<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> ResponseResult<T> success(String msg, T data) {
        return new ResponseResult<T>(MsgCodeEnum.SUCCESS.getCode(), msg, data);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getRecords() {
        return records;
    }

    public void setRecords(int records) {
        this.records = records;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}